Solving a parabolic problem

Solving a Parabolic problem

Let \(\Omega\) be the unit square \([0,1]^2\). Consider the following problem defined over \(\Omega\):

\[ \begin{cases} \frac{\partial f}{\partial t}-\Delta f = u &in \ \Omega \times [0,T] \\ \quad f = f_0 &in \ \Omega, \ t=0 \\ \quad f = 0 &on \ \partial \ \Omega,\ t>0 \end{cases} \]

where \(f_0 = \sin( 2\pi x)\sin(2\pi y)\) is the initial condition, \(u(x,y) = 8\pi^2 \sin( 2\pi x)\sin(2\pi y) e^{-t}\) is the forcing term and \(\partial \Omega\) is the boundary of \(\Omega\) where we have prescribed homogeneous Dirichelet boundary condition for all \(t\) grater than \(0\). The exact solution of the previous problem is \(u_{ex}(x,y) = \sin(2\pi x) \sin(2 \pi y) e^{-t}\) whose trend is shown in the following video.

## load domain data
data("unit_square", package="femR")

## time steps
t0 = 0.
t_max = 1
dT = 1e-2
M = (t_max - t0)/dT + 1
times = seq(t0,t_max,length=M)

## Spatio-temporal domain
domain = Mesh(unit_square) %X% times

## create Functional Space
Vh <- FunctionSpace(domain)

## define differential operator in its strong formulation
f <- Function(Vh)

## define differential operator in its strong formulation
L <- dt(f) - laplace(f)

## forcing term
u <- function(points,times){
  res <- matrix(0, nrow=nrow(points), ncol=length(times))
  for( t in 1:length(times)){
    res[,t] = (8*pi^2 -1) * sin( 2.* pi * points[,1]) * sin(2.*pi* points[,2]) * exp(-times[t])
  }
  return(res) 
}
## Dirichlet BC
dirichletBC <- function(points, times){
  return(matrix(0, nrow=nrow(points), ncol=length(times)))
}

## initial condition 
initialCondition <- function(points){
 return(sin( 2.* pi * points[,1]) * sin(2.*pi* points[,2]))
}
  
## create pde
pde <- Pde(L, u, dirichletBC, initialCondition)
#> tracemem[0x55f4439b93e0 -> 0x55f440a120f8]: Pde Pde eval eval eval_with_user_handlers withVisible withCallingHandlers handle timing_fn evaluate_call <Anonymous> evaluate in_dir in_input_dir eng_r block_exec call_block process_group.block process_group withCallingHandlers withCallingHandlers process_file <Anonymous> <Anonymous> vweave_rmarkdown <Anonymous> doTryCatch tryCatchOne tryCatchList tryCatch <Anonymous> <Anonymous> <Anonymous> do.call saveRDS withCallingHandlers doTryCatch tryCatchOne tryCatchList doTryCatch tryCatchOne tryCatchList tryCatch

## solve problem
pde$solve()

## plot solution
contour(f)